#include "stdafx.h"
#include "DialogInputPorts.h"
#include "Util.hpp"

DialogInputPorts::DialogInputPorts(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
}

optional<QString> DialogInputPorts::getLocalAddress()
{
	using namespace std::string_literals;
	using namespace Util;
	auto address = ui.edtLocalAddress->text();
	auto bOk = false;
	address.toUInt(&bOk);
	if (bOk)
		address = R"(http://localhost:)" + address;
	if (!start_with(address.toStdString(), R"(http://)"s) && !start_with(address.toStdString(), R"(https://)"s))
		address = R"(http://)" + address;
	if (!uri::validate(address.toStdWString()))
		return {};
	uri_builder builder(address.toStdWString());
	if (-1 == builder.port())
		return {};
	if (builder.host().empty())
		return {};
	if (builder.path() != U(R"(/)"))
		return {};
	return address;
}

optional<QString> DialogInputPorts::getRemoteAddress()
{
	using namespace std::string_literals;
	using namespace Util;
	auto address = ui.edtRemoteAddress->text();
	auto bOk = false;
	address.toUInt(&bOk);
	if (bOk)
		address = R"(http://localhost:)" + address;
	if (!start_with(address.toStdString(), R"(http://)"s) && !start_with(address.toStdString(), R"(https://)"s))
		address = R"(http://)" + address;
	if (!uri::validate(address.toStdWString()))
		return {};
	uri_builder builder(address.toStdWString());
	if (-1 == builder.port())
		return {};
	if (builder.host().empty())
		return {};
	if (builder.path() != U(R"(/)"))
		return {};
	return address;
}
